package com.ibm.testbed.script.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import com.ibm.testbed.script.editor.TestScript;

public class Validator {

	
	public static List<Label> labels = new ArrayList<Label>();

	public static void setMessageComposite(Composite parent) {
		Composite composite = new Composite(parent, 0);
		GridLayout layout = new GridLayout();
		layout.marginWidth = 0;
		layout.numColumns = 2;
		composite.setLayout(layout);
		Label label = new Label(composite, 0);
		label.setText("  ");
		Label label_message = new Label(composite, 0);
		labels.add(label_message);
		GridData data = new GridData(768);
		data.widthHint = 250;
		label_message.setLayoutData(data);
		Color bgColor = Display.getDefault().getSystemColor(
				SWT.COLOR_RED);
		label_message.setForeground(bgColor);
	}
	
	public static Boolean validator(Object obj,Object[] params){
		if(obj instanceof TestScript){
			TestScript testScript = (TestScript)obj;
			if(isNull(testScript.getParamDesc())){
				getMessageLabel().setText("Parameter Description must be input!");
				return false;
			}
			if(isNull(testScript.getParamFormat())){
				getMessageLabel().setText("Parameter Format must be input!");
				return false;
			}
			if(isNull(testScript.getParamExample())){
				getMessageLabel().setText("Parameter Example must be input!");
				return false;
			}
		}
		getMessageLabel().setText("");
		return true;
	}
	
	public static Label getMessageLabel(){
		for(int i=labels.size()-1;i>=0;i--){
			if(!labels.get(i).isDisposed()){
				return labels.get(i);
			}
		}
		return null;
	}
	public static Boolean isNull(String str){
		if(str == null || str.trim().equals("")){
			return true;
		}
		return false;
	}
	public static Boolean isPositiveInteger(String num){
		Pattern pattern = Pattern.compile("^[1-9]*[1-9][0-9]*$");
		Matcher matcher = pattern.matcher(num);
		return matcher.matches();
	}
	public static Boolean isInt(String num){
		Pattern pattern = Pattern.compile("^-?\\d+$");
		Matcher matcher = pattern.matcher(num);
		return matcher.matches();
	}
}
